package com.aloha.auth.interceptor;

import com.aloha.auth.domain.User;
import com.aloha.auth.utils.UserHolder;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

/**
 * @author zhdai
 * @date 2023/11/9 22:00
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        User user = new User().setId(1L).setUsername("dk1");
        user.setPerms(new ArrayList<String>() {{
            add("*");
//            add("test.*");
//            add("vin");
        }});
        UserHolder.setUser(user);
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        UserHolder.removeUser();
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }
}
